import type { DataFormItemProps, DataFormSchema } from '#/components/data-form/types';
import type { FieldProps, OnActionClickFn, RenderScope } from '#/components/data-table';

import { reactive } from 'vue';

import { $t } from '@croco/framework/locales';

export function useTable(onActionClick: OnActionClickFn): FieldProps[] {
    return [
        { prop: 'name', label: '名称', search: true, column: { width: 350, tooltip: true } },
        { prop: 'leader', label: '负责人', column: { minWidth: 120 } },
        { prop: 'phone', label: '联系号码', column: { minWidth: 120 } },
        { prop: 'sequence', label: '显示序号', column: { width: 90 } },
        { prop: 'createTime', label: '创建时间', column: { width: 190 } },
        { prop: 'modifyTime', label: '修改时间', column: { width: 190 } },
        {
            column: {
                width: 150,
                render: [
                    { auth: 'system:dept:add', onClick: (scope: RenderScope) => onActionClick({ code: 'add', scope }) },
                    { auth: 'system:dept:edit', onClick: (scope: RenderScope) => onActionClick({ code: 'edit', scope }) },
                    { auth: 'system:dept:remove', onClick: (scope: RenderScope) => onActionClick({ code: 'remove', scope }), type: 'danger' },
                ],
            },
        },
    ];
}

export function useForm(): DataFormSchema {
    const rules = reactive({
        name: [{ required: true, message: $t('ui.formRules.required'), trigger: 'blur' }],
        parentId: [{ required: true, message: $t('ui.formRules.required'), trigger: 'blur' }],
        phone: [{ pattern: /^1(3\d|4[014-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/, message: $t('ui.formRules.inputRequired', ['正确的11位手机号码']), trigger: 'blur' }],
    });
    const fields: DataFormItemProps[] = [
        { prop: 'id', primary: true },
        { prop: 'parentId', label: '上级部门' },
        { prop: 'name', label: '名称' },
        { prop: 'leader', label: '负责人' },
        { prop: 'phone', label: '联系号码' },
        { prop: 'sequence', label: '序号' },
    ];
    return { rules, fields };
}
